home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / english / dcodrle3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  3.8 KB  |  108 lines

  1. /* File: dcodrle3.c
  2.    Author: David Bourgin
  3.    Creation date: 1/2/94
  4.    Last update: 22/5/94
  5.    Purpose: Example of RLE type 3 decoding with a file source to decompress.
  6. */
  7.  
  8. #include <stdio.h>
  9. /* For routines printf,fgetc,fputc and fread */
  10. #include <stdlib.h>
  11. /* For routine exit */
  12.  
  13. /* Error codes sent to the caller */
  14. #define NO_ERROR      0
  15. #define BAD_FILE_NAME 1
  16. #define BAD_ARGUMENT  2
  17.  
  18. /* Useful constants */
  19. #define FALSE 0
  20. #define TRUE  1
  21.  
  22. /* Global variables */
  23. FILE *f_source,*f_dest;
  24.  
  25.                              /* Being that fgetc=EOF only after an access
  26.                                 then 'byte_stored_status' is 'TRUE' if a byte has been stored by 'fgetc'
  27.                                 or 'FALSE' if there's no valid byte not already read and not handled in 'val_byte_stored' */
  28. int byte_stored_status=FALSE;
  29. int val_byte_stored;
  30.  
  31. /* Pseudo procedures */
  32. #define end_of_data()  (byte_stored_status?FALSE:!(byte_stored_status=((val_byte_stored=fgetc(f_source))!=EOF)))
  33. #define read_byte()  (byte_stored_status?byte_stored_status=FALSE,(unsigned char)val_byte_stored:(unsigned char)fgetc(f_source))
  34. #define read_array(tableau,nb_a_lire)  ((void)fread((tableau),1,(nb_a_lire),f_source))
  35. #define write_byte(octet)  ((void)fputc((octet),f_dest))
  36. #define write_array(tableau,nb_octets_a_ecrire)  ((void)fwrite((tableau),1,(nb_octets_a_ecrire),f_dest))
  37.  
  38. void rle3decoding()
  39. /* Returned parameters: None
  40.    Action: Decompresses with RLE type 3 method all bytes read by the function read_byte
  41.    Erreurs: An input/output error could disturb the running of the program
  42. */
  43. { unsigned char header_byte,byte_read,nb_repetitions,
  44.                 raster[256];
  45.   register unsigned int frame_length,
  46.                         i;
  47.  
  48.   if (!end_of_data())
  49.      { header_byte=read_byte();
  50.        do {                  /* Being that header byte is present, then there are bytes to decompress */
  51.             byte_read=read_byte();
  52.             if (byte_read==header_byte)
  53.                              /* Encoding of a repetition of the header byte */
  54.                { nb_repetitions=read_byte();
  55.                  frame_length=((unsigned int)read_byte())+1;
  56.                  if (!nb_repetitions)
  57.                     for (i=1;i<=frame_length;i++)
  58.                         write_byte(header_byte);
  59.                  else { read_array(raster,frame_length);
  60.                         for (i=0;i<=nb_repetitions;i++)
  61.                             write_array(raster,frame_length);
  62.                       }
  63.                }
  64.             else write_byte(byte_read);
  65.           }
  66.        while (!end_of_data());
  67.      }
  68. }
  69.  
  70. void help()
  71. /* Returned parameters: None
  72.    Action: Displays the help of the program and the stop its running
  73.    Erreurs: None
  74. */
  75. { printf("This utility enables you to decompress a file by using RLE type 3 method\n");
  76.   printf("as given in 'La Video et Les Imprimantes sur PC'\n");
  77.   printf("\nUse: dcodrle3 source target\n");
  78.   printf("source: Name of the file to decompress\n");
  79.   printf("target: Name of the restored file\n");
  80. }
  81.  
  82. int main(argc,argv)
  83. /* Returned parameters: Returns an error code (0=None)
  84.    Action: Main procedure
  85.    Erreurs: Detected, handled and an error code is returned, if any
  86. */
  87. int argc;
  88. char *argv[];
  89. { if (argc!=3)
  90.      { help();
  91.        exit(BAD_ARGUMENT);
  92.      }
  93.   else if ((f_source=fopen(argv[1],"rb"))==NULL)
  94.           { help();
  95.             exit(BAD_FILE_NAME);
  96.           }
  97.        else if ((f_dest=fopen(argv[2],"wb"))==NULL)
  98.                { help();
  99.                  exit(BAD_FILE_NAME);
  100.                }
  101.             else { rle3decoding();
  102.                    fclose(f_source);
  103.                    fclose(f_dest);
  104.                  }
  105.   printf("Execution of dcodrle3 completed.\n");
  106.   return (NO_ERROR);
  107. }
  108.